home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DDJMAG / DDJ8801.ZIP / NARO.ZIP / FILES.C < prev    next >
Text File  |  1987-10-30  |  4KB  |  173 lines

  1. /*
  2.     Copyright (C) 1987 Paradigm Systems Inc.  All rights reserved.
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <fcntl.h>
  7. #include <sys\types.h>
  8. #include <sys\stat.h>
  9. #include <io.h>
  10. #include <string.h>
  11.  
  12. #include "loc.h"
  13. #include "externs.h"
  14.  
  15. #define    F_OPEN        O_RDONLY | O_BINARY
  16. #define    F_CREATE        O_CREAT | O_TRUNC | O_RDWR | O_BINARY
  17.  
  18.  
  19. void    open_file_system(input_file)
  20. char    *input_file ;
  21. {
  22.     char    *create_str = "Can't create %s" ;
  23.     char    *open_str = "Can't open %s" ;
  24.     char    errmsg[MAX_LINE] ;
  25.     char    *filename_ext ;
  26.  
  27.     /*
  28.         This module is responsible for openning or creating all of the
  29.         files used by this utility.
  30.     */
  31.  
  32.     /* Perform all the filename processing */
  33.     strcpy(module_name, strupr(input_file)) ;
  34.     strcpy(exe_fname, module_name) ;
  35.     strcat(strcpy(map_fname, exe_fname), ".MAP") ;
  36.  
  37.     if ((config == FALSE) || (strlen(config_fname) == 0))
  38.         strcat(strcpy(config_fname, exe_fname), ".CFG") ;
  39.     else
  40.         strupr(config_fname) ;
  41.     
  42.     if ((hex_name == FALSE) || (strlen(abs_fname) == 0))
  43.         strcat(strcpy(abs_fname, exe_fname), ".HEX") ;
  44.     else
  45.         strupr(abs_fname) ;
  46.  
  47.     strcat(strcpy(print_fname, exe_fname), ".LOC") ;
  48.     strcat(exe_fname, ".EXE") ;
  49.  
  50.     /* Create the temporary file used for segment fixups and location */
  51.     strcpy(tmp_fname, "LOCATE.$$$") ;
  52.     tmp_file = open(tmp_fname, F_CREATE, S_IWRITE) ;
  53.     if (tmp_file == -1)   {
  54.         sprintf(errmsg, create_str, tmp_fname) ;
  55.         perror(errmsg) ;
  56.         exit(1) ;
  57.     }
  58.  
  59.     /* Create the absolute output file */
  60.     abs_file = open(abs_fname, F_CREATE, S_IWRITE) ;
  61.     if (abs_file == -1)   {
  62.         sprintf(errmsg, create_str, abs_fname) ;
  63.         perror(errmsg) ;
  64.         exit(1) ;
  65.     }
  66.  
  67.     /* Open the .EXE file */
  68.    exe_file = open(exe_fname, F_OPEN) ;
  69.     if (exe_file == -1)   {
  70.         sprintf(errmsg, open_str, exe_fname) ;
  71.         perror(errmsg) ;
  72.         exit(1) ;
  73.     }
  74.  
  75.     /* Create the locate map output file */
  76.     print_file = fopen(print_fname, "wt") ;
  77.     if (print_file == NULL)   {
  78.         sprintf(errmsg, open_str, print_fname) ;
  79.         perror(errmsg) ;
  80.         exit(1) ;
  81.     }
  82.  
  83.     /* Open the configuration file for reading */
  84.     config_file = fopen(config_fname, "rt") ;
  85.     if (config_file == NULL)   {
  86.         sprintf(errmsg, open_str, config_fname) ;
  87.         perror(errmsg) ;
  88.         exit(1) ;
  89.     }
  90.  
  91.     /* Open the linker map file for reading */
  92.     map_file = fopen(map_fname, "rt") ;
  93.     if (map_file == NULL)   {
  94.         sprintf(errmsg, open_str, map_fname) ;
  95.         perror(errmsg) ;
  96.         exit(1) ;
  97.     }
  98.  
  99.       return ;
  100. }
  101.  
  102.  
  103. void     close_file_system()
  104. {
  105.     char    errmsg[MAX_LINE] ;
  106.     char    *close_str = "Unable to close %s" ;
  107.     char    *delete_str = "Unable to delete %s" ;
  108.  
  109.     /*
  110.         This function is responsible for shutting down the file system
  111.         and cleaning up the temporary files.  All files opened for
  112.         reading are closed and all files open for writing are closed
  113.         (normal exit) and possibly deleted (control-C abort event).
  114.     */
  115.  
  116.     /* Close the link map */
  117.     if (fclose(map_file) != 0)   {
  118.         sprintf(errmsg, close_str, map_fname) ;
  119.         perror(errmsg) ;
  120.     }
  121.  
  122.     /* Close the locate configuration file */
  123.     if (fclose(config_file) != 0)   {
  124.         sprintf(errmsg, close_str, config_fname) ;
  125.         perror(errmsg) ;
  126.     }    
  127.  
  128.     /* Close the .EXE file */
  129.     if (close(exe_file) == -1)   {
  130.         sprintf(errmsg, close_str, exe_fname) ;
  131.         perror(errmsg) ;
  132.     }
  133.  
  134.     /* Close the locate map */
  135.     if (fclose(print_file) != 0)   {
  136.         sprintf(errmsg, close_str, print_fname) ;
  137.         perror(errmsg) ;
  138.     }
  139.  
  140.     /* Close the absolute or hex object module */
  141.     if (close(abs_file) == -1)   {
  142.         sprintf(errmsg, close_str, abs_fname) ;
  143.         perror(errmsg) ;
  144.     }
  145.  
  146.     if (user_abort == TRUE)   {
  147.         /* Delete the locate map */
  148.         if (remove(print_fname) == -1)   {
  149.             sprintf(errmsg, delete_str, print_fname) ;
  150.             perror(errmsg) ;
  151.         }
  152.  
  153.         /* Delete the object file */
  154.         if (remove(abs_fname) == -1)   {
  155.             sprintf(errmsg, delete_str, abs_fname) ;
  156.             perror(errmsg) ;
  157.         }
  158.     }
  159.  
  160.     /* Close and then delete the temporary file */
  161.     if (close(tmp_file) == -1)   {
  162.         sprintf(errmsg, close_str, tmp_fname) ;
  163.         perror(errmsg) ;
  164.     }
  165.  
  166.     if (remove(tmp_fname) == -1)   {
  167.         sprintf(errmsg, delete_str, tmp_fname) ;
  168.         perror(errmsg) ;
  169.     }
  170.  
  171.     return ;
  172. }
  173.